<?php
use Phalcon\Mvc\Model\Validator\Uniqueness as Uniqueness;

use Phalcon\Mvc\Model\Validator\Email as Email;

class AdminAccount extends \Phalcon\Mvc\Model
{

    /**
     *
     * @var integer
     */
    public $modify;
    public $status;

    public $id;

    /**
     *
     * @var string
     */
    public $email;

    /**
     *
     * @var string
     */
    public $last_login;

    /**
     *
     * @var string
     */
    public $date_created;

    /**
     *
     * @var integer
     */
    public $active;

    /**
     *
     * @var string
     */
    public $actions;

    /**
     *
     * @var string
     */
    public $date_modified;

    /**
     *
     * @var integer
     */
    public $admin_type;

    /**
     *
     * @var string
     */
    public $username;

    /**
     *
     * @var string
     */
    public $password;

    /**
     *
     * @var string
     */
    public $fullname;

    /**
     *
     * @var integer
     */
    public $approve;

    /**
     *
     * @var integer
     */
    public $delete;

    /**
     *
     * @var integer
     */
    public $update;

    /**
     *
     * @var integer
     */
    public $add;
    public function validation()
    {

        $this->validate(
            new Email(
                array(
                    'field'    => 'email',
                    'required' => true,
                )
            )
        );
        if ($this->validationHasFailed() == true) {
            return false;
        }
    }

    public function beforeValidationOnCreate()
    {
        $this->status = 1;
        $this->date_created = CURR_DATE;
        $this->password = md5($this->password);
        if(!empty($this->fullname)) $this->fullname = ucwords($this->fullname);
        $this->validate(new Uniqueness(
          array(
                "field"   => "email",
                "message" => "The email is already registered please try another"
            )
        ));

        $this->validate(new Uniqueness(
          array(
                "field"   => "username",
                "message" => "The username is already registered please try another"
            )
        ));
        
        if ($this->validationHasFailed() == true)
        {
            return false;
        }
    }
    public function beforeValidationOnUpdate()
    {
        $this->date_modified = CURR_DATE;
        if(!empty($this->fullname)) $this->fullname = ucwords($this->fullname);
    }

    /**
     * Independent Column Mapping.
     */
    public function columnMap()
    {
        return array(
            'id' => 'id', 
            'email' => 'email', 
            'last_login' => 'last_login', 
            'date_created' => 'date_created', 
            'active' => 'active', 
            'actions' => 'actions', 
            'date_modified' => 'date_modified', 
            'admin_type' => 'admin_type', 
            'username' => 'username', 
            'password' => 'password', 
            'fullname' => 'fullname', 
            'approve' => 'approve', 
            'delete' => 'delete', 
            'update' => 'update', 
            'add' => 'add',
            'status' => 'status',
            'modify' => 'modify'
        );
    }



}
